package com.example.jsoupdemo.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

public class JsoupUtil {
    public static Document getDocument(){
        String url = "https://api.cnboy.top";
        try {
            // jsoup解析url，第二个参数是超时时间
            Document document = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36")
                    .get();
            return document;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        HashMap<String, String> apiStatus = new HashMap<>();
        Document document = getDocument();
        // 寻找所有class  = col-lg-4 的dom
        Elements elements = document.getElementsByClass("col-lg-4");

        for (Element element : elements){
            // 从以上的所有dom中的第一个dom中寻找标签h2的值
            String title = element.getElementsByTag("h2").get(0).text();
            // 从以上的dom中的第一个dom中寻找标签p
            String status = element.getElementsByTag("p").get(0).text().substring(7,10);
            apiStatus.put(title,status);
        }
        System.out.println(apiStatus);
    }
}
